﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PegasCompany.Data;
using PegasCompany.Helper;
using PegasCompany.Tools;
using PegasCompany.Tools.Text;

namespace PegasCompany.AdminPanel
{
    public partial class Infoblock : System.Web.UI.Page
    {
        private SiteContent CurrentInfoblock
        {
            get
            {
                object oCurrentInfoblock = ViewState["CurrentInfoblock"];
                return (oCurrentInfoblock != null) ? (SiteContent)oCurrentInfoblock : null;
            }
            set
            {
                ViewState["CurrentInfoblock"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!Request.QueryString.HasKeys())
                {
                    ShowInfoblockList();
                }
                else
                {
                    ShowInfoblockInfo();
                }
            }
        }

        private void ShowInfoblockList()
        {
            mvInfoblock.ActiveViewIndex = 0;

            List<SiteContent> lstInfoBlock = SiteContent.GetInfoBlockList((int)EnumHelper.ContentType.Block1, (int)EnumHelper.ContentType.Block2, (int)EnumHelper.ContentType.Block3);
            if (lstInfoBlock != null)
            {
                if (lstInfoBlock.Count != 3)
                {
                    SiteContent b1 =
                        lstInfoBlock.FirstOrDefault(i => i.ContentTypeCode == (int)EnumHelper.ContentType.Block1);
                    if (b1 == null)
                    {
                        b1 = new SiteContent();
                        b1.SaveEmpty((int) EnumHelper.ContentType.Block1, EnumHelper.ContentType.Block1.ToString());
                    }

                    SiteContent b2 =
                        lstInfoBlock.FirstOrDefault(i => i.ContentTypeCode == (int)EnumHelper.ContentType.Block2);
                    if (b2 == null)
                    {
                        b2 = new SiteContent();
                        b2.SaveEmpty((int) EnumHelper.ContentType.Block2, EnumHelper.ContentType.Block2.ToString());
                    }

                    SiteContent b3 =
                        lstInfoBlock.FirstOrDefault(i => i.ContentTypeCode == (int)EnumHelper.ContentType.Block3);
                    if (b3 == null)
                    {
                        b3 = new SiteContent();
                        b3.SaveEmpty((int) EnumHelper.ContentType.Block3, EnumHelper.ContentType.Block3.ToString());
                    }

                    ShowInfoblockList();
                }
                else
                {
                    rptInfoblocks.DataSource = lstInfoBlock;
                    rptInfoblocks.DataBind();
                }
            }
        }

        private void ShowInfoblockInfo()
        {
            mvInfoblock.ActiveViewIndex = 1;

            int iSiteContentId = PageHelper.GetIntValue(Request.QueryString["id"]);
            if (iSiteContentId == -1)
            {
                RedirectToDefaulPage();
            }
            SiteContent s = SiteContent.Get(iSiteContentId);
            if (s == null)
            {
                RedirectToDefaulPage();
                return;
            }

            CurrentInfoblock = s;

            txtInfoblockText.config.toolbar = PageHelper.EditorButtonSet;
            txtInfoblockText.config.width = "100%";

            txtInfoblockTitle.Text = s.Title;
            txtInfoblockText.Text = s.Content;
        }

        private void RedirectToDefaulPage()
        {
            Response.Redirect("/AdminPanel/Infoblock.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CurrentInfoblock == null)
            {
                RedirectToDefaulPage();
            }
            else
            {
                CurrentInfoblock.Title = txtInfoblockTitle.Text.Trim();
                CurrentInfoblock.Content = txtInfoblockText.Text.Trim();

                CurrentInfoblock.UpdateInstance();
                ShowInfoblockList();
            }
        }
    }
}